﻿using DeviceMaintainGIS.BLL;
using DeviceMaintainGIS.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace DeviceMaintainGIS.Controllers
{
    /// <summary>
    /// 公共方法
    /// </summary>
    public class CommonsController : ApiController
    {
        ///// <summary>
        /////上传图片
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public void UploadFileList()
        //{
        //    var response = new ReturnMessageVM();
        //    var pathList = new List<string>();
        //    //int maxsize = 0;
        //    //string size = ApiHelper.GetWebConfig("AppUpload_size", "5242880");// "524288000"
        //    //int.TryParse(size, out maxsize);
        //    //for (int i = 0; i < Request.Files.Count; i++)
        //    //{
        //    //    if (Request.Files[i].ContentLength > maxsize)
        //    //    {
        //    //        response.Succeed = false;
        //    //        response.Message = "上传文件大小超出限制";
        //    //        return Json(response);
        //    //    }
        //    //}
        //    string resultPath = "";
        //    if (Request.Files.Count == 0)
        //    {
        //        response.Succeed = false;
        //        response.Message = "未获取到文件流！";
        //        return Json(response);
        //    }
        //    for (int i = 0; i < Request.Files.Count; i++)
        //    {
        //        HttpPostedFileBase file = Request.Files[i];
        //        if (file != null && file.FileName != string.Empty)
        //        {
        //            string ext = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1).ToLower();
        //            string[] extendFileName = { "jpeg", "jpg", "gif", "png" };
        //            if (!extendFileName.Contains(ext))
        //            {
        //                response.Succeed = false;
        //                response.Message = "您传入的图片格式不正确！";
        //                return Json(response);
        //            }
        //            resultPath = "http://49.4.69.20:8081/Files/"+ file.FileName;
        //            try
        //            {
        //                file.SaveAs(resultPath);
        //                //resultPath = FastUpload.UploadVideo(file.InputStream, file.FileName, out errormessage);
        //                //pathList.Add(resultPath);
        //            }
        //            catch (Exception ex)
        //            {
        //                response.Succeed = false;
        //                response.Message = ex.Message;
        //                return Json(response);
        //            }
        //        }
        //    }
        //    response.Succeed = true;
        //    response.Message = resultPath;
        //    return Json(response);
        //}
        /// <summary>
        /// 根据参数删除图片
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public ReturnMessageVM Get([FromUri]string strUrl)
        {
            ReturnMessageVM rmm = new ReturnMessageVM();
            rmm = new CommonsService().DelImageFile(strUrl);
            return rmm;
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Post()
        {
            try
            {
                //判断是否是MIME类型
                if (Request.Content.IsMimeMultipartContent())
                {
                    this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
                }
                var UploadFolder = GetUploadFolder();

                HttpFileCollection files = HttpContext.Current.Request.Files;
                string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;

                List<UploadDataModel> UploadDataModelList = new List<UploadDataModel>();
                UploadDataModel model = new Controllers.UploadDataModel();
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    HttpPostedFile file = files[iFile];
                    string Time = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    string filePath = UploadFolder + "/" + Time + file.FileName;
                    file.SaveAs(BaseDirectory + filePath);
                    model.FileName = file.FileName;
                    model.FilePath = filePath;
                    model.FileSize = Convert.ToDecimal(file.ContentLength) / 1024 / 1024;
                    UploadDataModelList.Add(model);
                }
                return this.Request.CreateResponse(HttpStatusCode.OK, new { UploadDataModelList });
            }
            catch (Exception ex)
            {
                //Log.Error("FilesController", ex.ToString());
                return this.Request.CreateResponse(HttpStatusCode.OK, "出现错误！");
            }
        }
        private string GetUploadFolder()
        {
            //文件的上传路径
            //var uploadFolder = "FileUploads/" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月/" + DateTime.Now.ToLongDateString();
            var MonthDirectory = "FileUploads/" + DateTime.Now.ToString("yyyyMM");
            var uploadFolder = MonthDirectory + "/" + DateTime.Now.ToString("yyyyMMdd");

            //获取根路径
            var root = HttpContext.Current.Server.MapPath(uploadFolder);
            var MonthDirectoryRoot = HttpContext.Current.Server.MapPath(MonthDirectory);

            //创建文件夹
            Directory.CreateDirectory(MonthDirectoryRoot);
            Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + uploadFolder);

            return uploadFolder;
        }

    }
    public class UploadDataModel
    {
        /// <summary>
        /// 附件ID
        /// </summary>
        public int FileID { get; set; }
        /// <summary>
        /// 文件全路径
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }
        /// <summary>
        /// 文件大小
        /// </summary>
        public decimal FileSize { get; set; }
    }
}

